package cn.openea.perpartner.cms.web.service.impl;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.cms.web.entity.CmsCustomer;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.openea.perpartner.cms.factory.UserContext;
import cn.openea.perpartner.cms.web.dto.FavCommunityDto;
import cn.openea.perpartner.cms.web.dto.FavCustomerDto;
import cn.openea.perpartner.cms.web.entity.CmsCommunity;
import cn.openea.perpartner.cms.web.entity.CmsFavorites;
import cn.openea.perpartner.cms.web.mapper.CmsFavoritesMapper;
import cn.openea.perpartner.cms.web.service.ICmsFavoritesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * (CmsFavorites)表服务实现类
 *
 * @author Weird077
 * @since 2022-08-25 10:06:53
 */
@Service("favoritesService")
public class CmsFavoritesServiceImpl extends ServiceImpl<CmsFavoritesMapper, CmsFavorites> implements ICmsFavoritesService {
    private static final String CUSTOMER = "品牌商家";
    private static final String COMMUNITY = "团购团长";
    @Override
    public List<FavCustomerDto> selectCustomerList(String userId,String searchStr) {
        QueryWrapper<CmsCustomer> wrapper = new QueryWrapper<>();
        if(StrUtil.isBlank(userId)){
            userId = UserContext.getUserId();
        }
        StringBuffer queryStr = new StringBuffer();
        if (StrUtil.isNotBlank(searchStr)){
            queryStr.append("and (");
            queryStr.append("company like '%"+searchStr+"%'");
            queryStr.append("or category like '%"+searchStr+"%'");
            queryStr.append("or brand like '%"+searchStr+"%'");
            queryStr.append("or contacts like '%"+searchStr+"%'");
            queryStr.append("or telphone like '%"+searchStr+"%'");
            queryStr.append("or intention like '%"+searchStr+"%'");
            queryStr.append(")");
        }
        wrapper.last("left join cms_customer b on a.link_id = convert(b.unid,CHAR) where  a.create_user='"+ userId+"'  and b.unid <> '' "+ queryStr +" ");
        return baseMapper.selectCustomerList(wrapper);
    }

    @Override
    public List<FavCommunityDto> selectCommunityList(String userId,String searchStr) {
        QueryWrapper<CmsCommunity> wrapper = new QueryWrapper<>();
        if(StrUtil.isBlank(userId)){
            userId = UserContext.getUserId();
        }
        StringBuffer queryStr = new StringBuffer();
        if (StrUtil.isNotBlank(searchStr)){
            queryStr.append("and (");
            queryStr.append("b.openea_id like '%"+searchStr+"%'");
            queryStr.append("or b.cooperate_grade like '%"+searchStr+"%'");
            queryStr.append("or b.user_name like '%"+searchStr+"%'");
            queryStr.append("or b.category like '%"+searchStr+"%'");
            queryStr.append("or b.phone_num like '%"+searchStr+"%'");
            queryStr.append("or b.area like '%"+searchStr+"%'");
            queryStr.append("or b.address like '%"+searchStr+"%'");
            queryStr.append(")");
        }
        wrapper.last("left join cms_community b on a.link_id = convert(b.unid,CHAR) where  a.create_user='"+ userId+"' and b.unid <>  '' "+ queryStr +" ");
        return baseMapper.selectCommunityList(wrapper);
    }

    @Override
    public boolean check(CmsFavorites favorites) {
        LambdaQueryWrapper<CmsFavorites> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsFavorites::getLinkType,favorites.getLinkType());
        Long count = baseMapper.selectCount(wrapper);
        if(favorites.getLinkType().equals(CUSTOMER)){
            return count<10;
        }
        if(favorites.getLinkType().equals(COMMUNITY)){
            return count<100;
        }
        return false;
    }

    @Override
    public boolean checkUnique(List<CmsFavorites> favoritesList) {
        List<String> linkIdList = new ArrayList<>();
        favoritesList.forEach(favorites->linkIdList.add(favorites.getLinkId()));
        LambdaQueryWrapper<CmsFavorites> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CmsFavorites::getLinkId,linkIdList);
        return baseMapper.selectCount(wrapper)>0;
    }

    @Override
    public boolean deleteByLinkId(Long linkId, String userId) {
        LambdaQueryWrapper<CmsFavorites> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsFavorites::getLinkId, Convert.toStr(linkId));
        wrapper.eq(CmsFavorites::getCreateUser,userId);
        return baseMapper.delete(wrapper)>0;
    }
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean save(List<CmsFavorites> favoritesList) {
        boolean isSuccess = false;
        for (CmsFavorites favorites:favoritesList) {
            favorites.setCreateUser(UserContext.getUserId());
            isSuccess=save(favorites);
        }
        return isSuccess;
    }
}

